package service

import (
	"awesomeProject/app/model"
	"awesomeProject/app/request"
	"awesomeProject/app/response"
	"awesomeProject/global"
	"awesomeProject/util"
	"context"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"strings"
	"time"
)

type ShopTransSrv struct{}

func (s *ShopTransSrv) ShopTrans(req request.ShopTrans) response.Resp {
	filter := bson.D{{"shop_id", req.ShopId}}
	preFilter := bson.D{{"shop_id", req.ShopId}}
	if req.DateVal != "" {
		if req.DateType == 1 || req.DateType == 4 { // 日期
			dateVal, _ := time.Parse("2006-01-02", req.DateVal)
			preSDate := dateVal.AddDate(0, 0, -1)
			filter = append(filter, bson.E{Key: "date", Value: dateVal})
			preFilter = append(preFilter, bson.E{Key: "date", Value: preSDate})
		} else if req.DateType == 2 || req.DateType == 3 { // 近7天or近30天
			dateRange := strings.Split(req.DateVal, "~")
			subDays := -7
			if req.DateType == 3 {
				subDays = -30
			}
			sDate, _ := time.Parse("2006-01-02", dateRange[0])
			eDate, _ := time.Parse("2006-01-02", dateRange[1])
			preSDate := sDate.AddDate(0, 0, subDays)
			preEDate := eDate.AddDate(0, 0, subDays)
			filter = append(filter, bson.E{Key: "date", Value: bson.M{"$gte": sDate, "$lte": eDate}})
			preFilter = append(preFilter, bson.E{Key: "date", Value: bson.M{"$gte": preSDate, "$lte": preEDate}})
		} else if req.DateType == 5 { // 月份
			month, _ := time.Parse("2006-01", req.DateVal)
			preMonth := month.AddDate(0, -1, 0)
			sd := util.GetMonthStartAndEnd(month.Year(), int(month.Month()))
			preSd := util.GetMonthStartAndEnd(preMonth.Year(), int(preMonth.Month()))
			filter = append(filter, bson.E{Key: "date", Value: bson.M{"$gte": sd["start"], "$lte": sd["end"]}})
			preFilter = append(preFilter, bson.E{Key: "date", Value: bson.M{"$gte": preSd["start"], "$lte": preSd["end"]}})
		}
	}

	match := bson.D{{
		"$match", filter,
	}}
	group := bson.D{{
		"$group", bson.D{
			{"_id", nil},
			{"shop_id", bson.M{"$last": bson.M{"$toInt": "$shop_id"}}},
			{"item_cart_uv", bson.M{"$sum": bson.M{"$toInt": "$item_cart_uv.content"}}},
			{"cart_item_num", bson.M{"$sum": bson.M{"$toInt": "$cart_item_num.content"}}},
			{"item_verified_cnt", bson.M{"$sum": bson.M{"$toInt": "$item_verified_cnt.content"}}},
			{"ctm_item_view_uv", bson.M{"$sum": bson.M{"$toInt": "$ctm_item_view_uv.content"}}},
			{"order_buyer_cnt", bson.M{"$sum": bson.M{"$toInt": "$order_buyer_cnt.content"}}},
			{"ctm_item_view_pv", bson.M{"$sum": bson.M{"$toInt": "$ctm_item_view_pv.content"}}},
			{"item_verified_uv", bson.M{"$sum": bson.M{"$toInt": "$item_verified_uv.content"}}},
			{"order_item_num", bson.M{"$sum": bson.M{"$toInt": "$order_item_num.content"}}},
			{"item_order_usd", bson.M{"$sum": bson.M{"$toDouble": "$item_order_usd.content"}}},
			{"item_verified_usd", bson.M{"$sum": bson.M{"$toDouble": "$item_verified_usd.content"}}},
		},
	}}
	cursor, err := global.MongoDB.Collection("shop_trans").Aggregate(context.TODO(), mongo.Pipeline{match, group})
	if err != nil {
		return response.ErrQuery.SetMsg(err.Error())
	}
	var trans []model.ShopTrans
	if err = cursor.All(context.TODO(), &trans); err != nil {
		return response.ErrQuery.SetMsg(err.Error())
	}

	preMatch := bson.D{{
		"$match", preFilter,
	}}
	preCursor, err := global.MongoDB.Collection("shop_trans").Aggregate(context.TODO(), mongo.Pipeline{preMatch, group})
	if err != nil {
		return response.ErrQuery.SetMsg(err.Error())
	}
	var preTrans []model.ShopTrans
	if err = preCursor.All(context.TODO(), &preTrans); err != nil {
		return response.ErrQuery.SetMsg(err.Error())
	}
	if len(trans) == 0 {
		trans = append(trans, model.ShopTrans{})
	}
	if len(preTrans) == 0 {
		preTrans = append(preTrans, model.ShopTrans{})
	}
	FTrans, preFTrans := trans[0], preTrans[0]

	// 客单价=订单确认金额/订单确认人数
	FTrans.PerCustomerValue = util.Div(FTrans.ItemVerifiedUsd, FTrans.ItemVerifiedUv)
	preFTrans.PerCustomerValue = util.Div(preFTrans.ItemVerifiedUsd, preFTrans.ItemVerifiedUv)

	// 加购-下单转化率=下单买家数/加购人数
	FTrans.OrderCartRate = util.Div(FTrans.OrderBuyerCnt, FTrans.ItemCartUv)
	preFTrans.OrderCartRate = util.Div(preFTrans.OrderBuyerCnt, preFTrans.ItemCartUv)

	// 下单转化率=下单买家数/商品访客数
	FTrans.CtmOrderViewRate = util.Div(FTrans.OrderBuyerCnt, FTrans.CtmItemViewUv)
	preFTrans.CtmOrderViewRate = util.Div(preFTrans.OrderBuyerCnt, preFTrans.CtmItemViewUv)

	// 浏览-订单确认转化率=订单确认人数/商品访客数
	FTrans.ViewItemVerifiedRate = util.Div(FTrans.ItemVerifiedUv, FTrans.CtmItemViewUv)
	preFTrans.ViewItemVerifiedRate = util.Div(preFTrans.ItemVerifiedUv, preFTrans.CtmItemViewUv)

	// 加购率=加购人数/商品访客数
	FTrans.CartRate = util.Div(FTrans.ItemCartUv, FTrans.CtmItemViewUv)
	preFTrans.CartRate = util.Div(preFTrans.ItemCartUv, preFTrans.CtmItemViewUv)

	FTrans.ItemCartUvCompare = util.IncreasePercent(preFTrans.ItemCartUv, FTrans.ItemCartUv)
	FTrans.CartItemNumCompare = util.IncreasePercent(preFTrans.CartItemNum, FTrans.CartItemNum)
	FTrans.ItemVerifiedCntCompare = util.IncreasePercent(preFTrans.ItemVerifiedCnt, FTrans.ItemVerifiedCnt)
	FTrans.CtmItemViewUvCompare = util.IncreasePercent(preFTrans.CtmItemViewUv, FTrans.CtmItemViewUv)
	FTrans.OrderBuyerCntCompare = util.IncreasePercent(preFTrans.OrderBuyerCnt, FTrans.OrderBuyerCnt)
	FTrans.CtmItemViewPvCompare = util.IncreasePercent(preFTrans.CtmItemViewPv, FTrans.CtmItemViewPv)
	FTrans.ItemVerifiedUvCompare = util.IncreasePercent(preFTrans.ItemVerifiedUv, FTrans.ItemVerifiedUv)
	FTrans.OrderItemNumCompare = util.IncreasePercent(preFTrans.OrderItemNum, FTrans.OrderItemNum)
	FTrans.CtmOrderViewRateCompare = util.IncreasePercent(preFTrans.CtmOrderViewRate, FTrans.CtmOrderViewRate)
	FTrans.CartRateCompare = util.IncreasePercent(preFTrans.CartRateCompare, FTrans.CartRateCompare)
	FTrans.ViewItemVerifiedRateCompare = util.IncreasePercent(preFTrans.ViewItemVerifiedRate, FTrans.ViewItemVerifiedRate)
	FTrans.OrderCartRateCompare = util.IncreasePercent(preFTrans.ItemCartUv, FTrans.ItemCartUv)
	FTrans.ItemVerifiedUsdCompare = util.IncreasePercent(preFTrans.ItemVerifiedUsd, FTrans.ItemVerifiedUsd)
	FTrans.ItemOrderUsdCompare = util.IncreasePercent(preFTrans.ItemOrderUsd, FTrans.ItemOrderUsd)
	FTrans.PerCustomerValueCompare = util.IncreasePercent(preFTrans.PerCustomerValue, FTrans.PerCustomerValue)

	return response.OK.WithData(FTrans)
}
